﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.WP.DocumentCenter.UserRadicadosViewer
{
    [ToolboxItemAttribute(false)]
    public class UserRadicadosViewer : WebPart, IWebPartPageComponentProvider
    {
        private Microsoft.SharePoint.WebPartPages.XsltListViewWebPart _Lvwp;

        public UserRadicadosViewer()
        {
            _Lvwp = new Microsoft.SharePoint.WebPartPages.XsltListViewWebPart();
            //this.ID = "wpRadicadosViewer";
        }

        protected override void OnInit(EventArgs e)
        {

            //Configure Ribbon
            /*
            Page currentPage = this.Page;
            SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
            SPContext context = SPContext.Current;
            ribbon.MakeTabAvailable(SPRibbon.LibraryTabId); //Notice the use of a static variable rather than static text
            */
            SPList library = ListHelper.GetUserListForCurrentUser();
            if (library != null)
            {
                SPContext.Current.Web.AllowUnsafeUpdates = true;
                SPView view = library.DefaultView;
                view.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>26</Value></Eq></Where>";
                view.Update();
                _Lvwp.ListName = library.ID.ToString("B").ToUpper();
                _Lvwp.ViewGuid = view.ID.ToString("B").ToUpper();
                _Lvwp.SuppressWebPartChrome = true;


                //Modifying ListView WebPart as changed SPView
                //oListViewWP.ViewGuid = view.ID.ToString("B").ToUpper();
                //oListViewWP.GetDesignTimeHtml();
                Controls.Add(_Lvwp);
                Title = library.Title;
                TitleUrl = view.ServerRelativeUrl;
                string wpcid = SPRibbon.GetWebPartPageComponentId(this);
                string lvwpcid = SPRibbon.GetWebPartPageComponentId(_Lvwp);
                string currentId = this.ID;
                string s = string.Format(@"
                <script type=""text/javascript"">

                $(document).ready(function(){{
                        selectLibraryWebPart();
                        $(document).click(function(event){{
                            selectLibraryWebPart();
                            event.stopPropagation(); 
                        }});
                 }});

                function selectLibraryWebPart()
                {{
                    var elem = $(""td[id$='_{0}']"");
                    if(elem.length > 0) {{
                        var dummyevent = new Array();
                        dummyevent['target'] = elem[0];
                        dummyevent['srcElement'] = elem[0];
                        WpClick(dummyevent);
                    }}
                    var wp = document.getElementById('_wpSelected');
                    if (wp != null)
	                {{                 
                        wp.value ='MSOZoneCell_WebPartctl00_m_{0}';
		                SelectRibbonTab('MSIM', true);
	                }}
                              
                }}", currentId);
    
                s +=@"Type.registerNamespace('EmbeddedListViewWebPart');
                var _webPartPageComponentId;
                var _lvwpPC;
                EmbeddedListViewWebPart.CustomPageComponent = function EmbeddedListViewWebPart_CustomPageComponent(webPartPcId,lvwpPc){
                    this._webPartPageComponentId = webPartPcId;
                    this._lvwpPC = lvwpPc;
                    EmbeddedListViewWebPart.CustomPageComponent.initializeBase(this);
                }
                EmbeddedListViewWebPart.CustomPageComponent.prototype = {
                    init: function EmbeddedListViewWebPart_CustomPageComponent$init() { },
                    getFocusedCommands: function EmbeddedListViewWebPart_CustomPageComponent$getFocusedCommands() { return this._lvwpPC.getFocusedCommands(); },
                    getGlobalCommands: function EmbeddedListViewWebPart_CustomPageComponent$getGlobalCommands() { return this._lvwpPC.getGlobalCommands(); },
                    isFocusable: function EmbeddedListViewWebPart_CustomPageComponent$isFocusable() { return this._lvwpPC.isFocusable(); },
                    canHandleCommand: function EmbeddedListViewWebPart_CustomPageComponent$canHandleCommand() { return this._lvwpPC.canHandleCommand(); },
                    handleCommand: function EmbeddedListViewWebPart_CustomPageComponent$handleCommand() { return this._lvwpPC.handleCommand(); },
                    getId: function EmbeddedListViewWebPart_CustomPageComponent$getId() { return this._webPartPageComponentId; },
                    yieldFocus: function EmbeddedListViewWebPart_CustomPageComponent$yieldFocus() { return this._lvwpPC.yieldFocus(); },
                    receiveFocus: function EmbeddedListViewWebPart_CustomPageComponent$receiveFocus() { return this._lvwpPC.receiveFocus(); }
                }
                function fixEmbeddedListView() {
                    EmbeddedListViewWebPart.CustomPageComponent.registerClass('EmbeddedListViewWebPart.CustomPageComponent', CUI.Page.PageComponent);
                    var pageMan = SP.Ribbon.PageManager.get_instance();
                    var pageComponent = pageMan.getPageComponentById('" + lvwpcid + @"');
                    var _cpc = new EmbeddedListViewWebPart.CustomPageComponent('" + wpcid + @"',pageComponent);
                    pageMan.addPageComponent(_cpc);
                }
                ExecuteOrDelayUntilScriptLoaded(fixEmbeddedListView, 'sp.ribbon.js');
                </script>";
                Controls.Add(new LiteralControl(s));
                this.Focus();
            }
            else
            {
                Controls.Add(new LiteralControl("Please select a library."));
            }
            this.ChromeType = PartChromeType.None;
            base.OnInit(e);
        }
        public WebPartContextualInfo WebPartContextualInfo
        {
            get
            {
                return _Lvwp.WebPartContextualInfo;
            }
        }
    }
}
